<template>
    <h1>理财项目信息添加</h1>

    <table>
        <tr>
            <td>理财名称</td>
            <td>
                <input type="text" v-model="AddFinancing.F_Name" placeholder="请输入理财名称">
            </td>
        </tr>

        <tr>
            <td>年收益</td>
            <td>
                <input type="text" v-model="AddFinancing.F_APY" placeholder="请输入年收益">
            </td>
        </tr>

        <tr>
            <td>最低金额</td>
            <td>
                <input type="text" v-model="AddFinancing.F_MinMoney" placeholder="请输入最低金额">
            </td>
        </tr>

        <tr>
            <td>风险等级</td>
            <td>

                <!-- <select v-model="AddFinancing.F_Level">
                    <option value="0">低风险</option>
                    <option value="1">中风险</option>
                    <option value="2">高风险</option>
                </select> -->

                <input type="radio" value="0" name="F_Level" v-model="AddFinancing.F_Level" checked/>低风险
                <input type="radio" value="1" name="F_Level" v-model="AddFinancing.F_Level" />中风险
                <input type="radio" value="2" name="F_Level" v-model="AddFinancing.F_Level" />高风险
            </td>
        </tr>

        <tr>
            <td>理财利息</td>
            <td>
                <select v-model="AddFinancing.T_Id">
                    <option value="0">==请选择==</option>
                    <option v-for="item in Interest" :value="item.T_Id" :key="item.T_Id">
                        {{item.T_Year}}年的利率为{{ item.T_Interesting }}%
                    </option>
                </select>
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input  type="button" value="添加" @click="AddData">
            </td>
        </tr>

    </table>

</template>

<script setup lang="ts">

import { ref,onMounted } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';

const router = useRouter();

//用于添加的响应式对象
const AddFinancing = ref({
    F_Name:'',
    F_APY:'',
    F_MinMoney:'',
    F_Level:'',
    T_Id:0
});

const Interest = ref([{
    T_Id:1,
    T_Year:'',
    T_Interesting:'',
}])

onMounted(()=>{
    ShowInterest();
})

const ShowInterest = ()=>{
    axios({
        method:'get',
        url:'http://localhost:53279/api/Financing/ShowInterest',

    })
    .then(function(res){
        
        console.log(res.data);
        
        Interest.value = res.data;
    })
    .catch(function(err){
        console.log(err);
    })
}

const AddData = ()=>{

    if(AddFinancing.value.F_Name==""){
        alert("请输入理财名称");
        return;
    }
    if(AddFinancing.value.F_APY==""){
        alert("请输入年收益");
        return;
    }
    if(AddFinancing.value.F_MinMoney==""){
        alert("请输入最低金额");
        return;
    }
    if( Number(AddFinancing.value.F_MinMoney)<=0 || Number(AddFinancing.value.F_MinMoney)>=10000000 ){
        alert("最低金额必须大于0并且10000000");
        return;
    }


    axios({
        method:'post',
        url:'http://localhost:53279/api/Financing/AddFinancing',
        data:{
            F_Name:AddFinancing.value.F_Name,
            F_APY:AddFinancing.value.F_APY,
            F_MinMoney:AddFinancing.value.F_MinMoney,
            F_Level:AddFinancing.value.F_Level,
            T_Id:AddFinancing.value.T_Id
        },
    })
.then(function(res){
    if(res.data == 1){
        alert('添加成功');
        router.push('/ShowFinancing');
    }
    else if(res.data == -1){
        alert('理财项目名称存在,请重新输入');
        return;
    }
    else{
        alert('添加失败');
        return;
    }
})

}
</script>